import 'package:bitcoin_mine/api/api.dart';
import 'package:bitcoin_mine/common/global.dart';
import 'package:bitcoin_mine/model/withdraw_info.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:flutter/services.dart';

import '../http/http_request.dart';

class WithdrawController extends GetxController {
  // 提现方式
  final RxString selectedMethod = 'Bitcoin Lightning'.obs;

  // 提现地址
  final RxString withdrawalAddress = ''.obs;

  // 提现金额
  final RxString withdrawalAmount = ''.obs;

  // 当前余额
  final RxString currentBalance = '0.000040000000000'.obs;

  // 提现规则
  final RxString minWithdrawal = '0.000000100000000'.obs;
  final RxString maxWithdrawal = '0.000040000000000'.obs;
  final RxString withdrawalFee = '0.000000050000000'.obs;

  // 提现方式列表
  final List<String> withdrawalMethods = [];

  @override
  void onInit() {
    super.onInit();
    withdrawalMethods.add(
      Global.mineInfo.value?.withdraw_links.lightning ?? 'Bitcoin Lightning',
    );
    withdrawalMethods.add(
      Global.mineInfo.value?.withdraw_links.onchain ?? 'Bitcoin Chain',
    );
    selectedMethod.value = withdrawalMethods.first;
  }

  // 弹窗显示状态
  final RxBool showMethodSelector = false.obs;

  // 设置提现地址
  void setWithdrawalAddress(String address) {
    withdrawalAddress.value = address;
  }

  // 设置提现金额
  void setWithdrawalAmount(String amount) {
    withdrawalAmount.value = amount;
  }

  // 选择提现方式
  void selectWithdrawalMethod(String method) {
    selectedMethod.value = method;
    showMethodSelector.value = false;
  }

  // 关闭弹窗
  void closeMethodSelector() {
    showMethodSelector.value = false;
  }

  // 复制地址
  void copyAddress() {
    if (withdrawalAddress.value.isNotEmpty) {
      Clipboard.setData(ClipboardData(text: withdrawalAddress.value));
      Get.snackbar('提示', '地址已复制到剪贴板', snackPosition: SnackPosition.BOTTOM);
    }
  }

  // 使用全部余额
  void useAllBalance() {
    withdrawalAmount.value = currentBalance.value;
  }

  // 执行提现
  void executeWithdrawal() {
    if (withdrawalAddress.value.isEmpty) {
      Get.snackbar('错误', '请输入提现地址', snackPosition: SnackPosition.BOTTOM);
      return;
    }

    if (withdrawalAmount.value.isEmpty) {
      Get.snackbar('错误', '请输入提现金额', snackPosition: SnackPosition.BOTTOM);
      return;
    }

    final amount = double.tryParse(withdrawalAmount.value);
    final minAmount = double.tryParse(minWithdrawal.value);
    final maxAmount = double.tryParse(maxWithdrawal.value);

    if (amount == null) {
      Get.snackbar('错误', '请输入有效的提现金额', snackPosition: SnackPosition.BOTTOM);
      return;
    }

    // if (minAmount != null && amount < minAmount) {
    //   Get.snackbar('错误', '提现金额不能小于最小提现金额', snackPosition: SnackPosition.BOTTOM);
    //   return;
    // }

    // if (maxAmount != null && amount > maxAmount) {
    //   Get.snackbar('错误', '提现金额不能大于最大提现金额', snackPosition: SnackPosition.BOTTOM);
    //   return;
    // }

    // HttpRequest.instance
    //     .requestCommon(
    //       "GET",
    //       Api.withdraw,
    //       queryParameters: {
    //         'address': withdrawalAddress.value,
    //         'amount': amount.toString(),
    //         'link': selectedMethod.value,
    //       },
    //     )
    //     .then((value) {
    //       print('提现成功: $value');
    //     });

    HttpRequest.instance.requestNetwork<List<WithdrawInfo>>(
      Api.withdraw,
      method: "GET",
      queryParameters: {
        'address': withdrawalAddress.value,
        'amount': amount.toString(),
        'link': selectedMethod.value,
      },
      onSuccess: (data) {
        // 执行提现逻辑
        // Get.snackbar(
        //   '成功',
        //   '提现申请已提交，请等待处理',
        //   snackPosition: SnackPosition.BOTTOM,
        // );
        // try {
        // Get.snackbar(
        //   '提示',
        //   '邀请链接已复制到剪贴板',
        //   snackPosition: SnackPosition.BOTTOM,
        // );
        Fluttertoast.showToast(msg: '提现申请已提交，请等待处理');
        // } catch (e) {
        //   print('bug: $e');
        // }
        // 返回上一页
        Get.back();
      },
      onError: (code, message) {
        // print('提现失败: $code, $message');
        // Get.snackbar(
        //   '错误',
        //   '提现失败: $message',
        //   snackPosition: SnackPosition.BOTTOM,
        // );
        Fluttertoast.showToast(msg: '提现失败: $message');
      },
    );
  }

  // 返回上一页
  void goBack() {
    Get.back();
  }
}
